## SPx model with even more prior weight on the ind component

model {
	
	# likelihood
	for (i in 1:(H)) {
		y[i] ~ dbinom(psi[i], n[i])
	
		psi[i] <- ilogit(theta[i])
	}

	y[H+1] ~ dbinom(psi[H+1], n[H+1])
	psi[H+1] <- ilogit(theta_new)
	
	# regression prior for historical response rates
	for (i in 1:H) {
		theta[i] ~ dnorm(inprod(x[i,], beta), tau.sq.inv)
	}
	
	# prior for new trial response rates
	theta_new = theta_hist + theta_reg + theta_null
	
	theta_reg_lat ~ dnorm(inprod(x[H+1,], beta), tau.sq.inv*25)   # if regression is good at estimating response rates from other trials then tau.sq.inv will be small and this component will be tighter
	theta_reg = theta_reg_lat*(M == 2)	

	theta_hist_lat ~ dnorm(mu_hist, sigma.sq.inv)
	mu_hist = inprod(w, theta[1:H])
	theta_hist = theta_hist_lat*(M == 1)
	
	psi_new_null_lat ~ dbeta(0.5, 0.5)
	psi_new_null = psi_new_null_lat
	theta_null = logit(psi_new_null)*(M == 3)
	theta_null_lat = logit(psi_new_null_lat)
	
	# definition of weights for mu_hist
	for(i in 1:(H+1)) {
		reg_prob[i] = ilogit(inprod(x[i,], beta))
	}
	
	alpha = pow(0.5, abs(reg_prob[1:H] - reg_prob[H+1])/0.05)
	
	w = alpha / (sum(alpha)) 
	
	# prior on z (which model is used)
	M ~ dcat(c(3/40, 3/40, 34/40))
	z = c((M == 1), (M == 2), (M == 3))
		
	# prior on regression coefficients
	for (i in 1:p) {
		beta[i] ~ dt(0, pow(2.5, -2), 1)
	}
	
	# prior for variance components
	tau ~ dt(0, pow(2.5, -2), 1) T(0,)   # half cauchy prior, not particularly informative
	tau.sq.inv <- pow(tau, -2)

	sigma ~ dt(0, pow(0.02, -2), 1) T(0,)   # half cauchy prior, not sure about scale here
	sigma.sq.inv <- pow(sigma, -2)
}




